package com.xk857.config;

import com.alibaba.fastjson.JSON;
import com.xk857.service.impl.JwtUser;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author CV大魔王
 * @Description jwt Token 扩展
 * @date 2021/2/27 10:33
 */
@Component
public class JwtTokenEnhancer implements TokenEnhancer {


    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        // 扩展令牌内容
        JwtUser user = (JwtUser) authentication.getPrincipal();
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("userInfo", JSON.toJSON(user));
        //设置附加信息
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(map);
        return accessToken;
    }
}
